package com.tksflysun.im.api.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tksflysun.im.api.bean.UserReq;
import com.tksflysun.im.api.redis.VerifyCodeRedisCache;
import com.tksflysun.im.api.service.UserService;
import com.tksflysun.im.common.bean.User;
import com.tksflysun.im.common.result.ControllerResult;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(path = "/register", method = RequestMethod.POST)
    public ControllerResult register(@RequestBody UserReq user) {
        ControllerResult result = ControllerResult.getSuccessResult();
        String existsVerifyCode = VerifyCodeRedisCache.getInstance().getT(user.getMobilePhone(), String.class);
        if (!user.getVerifyCode().toLowerCase().equals(existsVerifyCode)) {
            result.setCode(ControllerResult.VERIFY_CODE_OUT);
            return result;
        }
        User user2 = new User();
        BeanUtils.copyProperties(user, user2);
        userService.addUser(user2);
        return result;
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public ControllerResult update(@RequestBody UserReq user) {
        ControllerResult result = ControllerResult.getSuccessResult();
        User user2 = new User();
        BeanUtils.copyProperties(user, user2);
        userService.updateUser(user2);
        return result;
    }

}
